package com.evildog.workspace.life.application.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evildog.workspace.dependencies.mybatis.base.TableLinkService;
import com.evildog.workspace.dependencies.mybatis.object.QueryConditionDTO;
import com.evildog.workspace.life.application.bo.NoteBO;
import com.evildog.workspace.life.application.bo.NoteListBO;
import com.evildog.workspace.life.application.convert.NoteConvert;
import com.evildog.workspace.life.application.convert.NoteTagConvert;
import com.evildog.workspace.life.application.dto.NoteDTO;
import com.evildog.workspace.life.application.dto.NoteListDTO;
import com.evildog.workspace.life.application.entity.Note;
import com.evildog.workspace.life.application.entity.NoteListDO;
import com.evildog.workspace.life.application.entity.NoteTag;
import com.evildog.workspace.life.application.mapper.NoteMapper;
import com.evildog.workspace.life.application.mapper.NoteTagMapper;
import com.evildog.workspace.life.application.service.NoteService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements NoteService {

    private NoteTagMapper noteTagMapper;
    private TableLinkService tableLinkService;

    @Override
    public List<NoteListBO> getList(List<QueryConditionDTO> queryCondition, NoteListDTO dto) {
        List<NoteListDO> noteList = this.baseMapper.getList(queryCondition, dto);
        return NoteConvert.INSTANCE.convert(noteList);
    }

    @Override
    public NoteBO getDetailById(Long id) {
        Note note = this.getById(id);
        Assert.notNull(note, "note is null!");

        List<NoteTag> noteTags = tableLinkService.getLinkList(noteTagMapper, NoteTag::getNoteId, note.getId());


        NoteBO noteBO = NoteConvert.INSTANCE.convert(note);
        noteBO.setTags(NoteTagConvert.INSTANCE.convertBOList(noteTags));

        return noteBO;
    }

    @Override
    public NoteBO save(NoteDTO dto) {
        Note newNote = NoteConvert.INSTANCE.convert(dto);

        String content = dto.getContent();
        if (StrUtil.isNotBlank(content)) {
            String cleanHtml = HtmlUtil.cleanHtmlTag(content);
            int subLength = Math.min(cleanHtml.length(), 32);
            newNote.setAbstractContent(cleanHtml.substring(0, subLength));
        }

        // 设置笔记的初始标题
        String name = dto.getName();
        dto.setName(StrUtil.isBlank(name) ? "无标题笔记" : name);

        save(newNote);
        log.debug(newNote.getId() + "");

        return NoteConvert.INSTANCE.convert(newNote);
    }

    @Override
    public boolean remove(Long id) {

        // 删除笔记关联的标签
        return false;
    }

    @Override
    public boolean updateById(Long id, NoteDTO dto) {
        Note oldNote = getById(id);
        Assert.notNull(oldNote, "note is null!");
        Note newNote = NoteConvert.INSTANCE.convert(dto);

        String content = dto.getContent();
        if (StrUtil.isNotBlank(content)) {
            String cleanHtml = HtmlUtil.cleanHtmlTag(content);
            int subLength = Math.min(cleanHtml.length(), 32);
            newNote.setAbstractContent(cleanHtml.substring(0, subLength));
        }

        newNote.setId(id);
        return updateById(newNote);
    }
}
